//paraDefLis主页面
$(function(){
		var paraDefLis;
		paraDefLis=$("#ParaDefLis").datagrid({
		url:'/admin/allDef',
  		toolbar:'#toolbar',
  		fit : true,
		fitColumns : true,
  		striped:true,
  		singleSelect:'true',
		pagination:'true',
		idField : 'subjecttype_id',
		rownumbers : true,
		columns : [ [
		{field:'checkbox',title:'check',width:10, checkbox:true },
		
		{field:'PARA_ID', title:'paraId', width:40 },
		{field:'PARA_NAME', title:'paraName', width:40 },
		{field:'PARA_DESC', title:'paraDesc', width:40 },
		{field:'DATA_TYPE', title:'dataType', width:40 },
		{field:'SYSTEM_ADMIN', title:'systemAdmin', width:40 },
		{field:'ORGAN_RELA', title:'organRela', width:40 },
		{field:'DEPT_RELA', title:'deptRela', width:40 },
		{field:'TIME_RELA', title:'timeRela', width:40 },
		{field:'SINGLE_PARA_VALUE', title:'singleParaValue', width:40 },	
			]]
		});			
		
		
		
		
		
		
		//修改参数信息弹窗
		var flag1=false;
		var flag2=false;
		var flag3=false;
		
		var eidtParamDialog;
		eidtParamDialog=$("#eidtParamDialog").dialog({
		title : '修改参数',
		width : fixWidth(0.7),
		height : fixHeight(0.8),
		closed : true,											
		cache : false,
		iconCls : 'icon-add',									
		modal : true,
		resizable:true,
		onClose:function(){						
			$("#eidtParamForm").form("clear");
			$("#singleParaValue").attr("disabled",false);
			var flag1=false;
			var flag2=false;
			var flag3=false;
			$("#btnAddValue").linkbutton('enable');
		},		
		buttons : [ {							
			iconCls : 'icon-ok',
			text : '提交',
			handler : function() {				
					$("#eidtParamForm").form('submit', {	
						url : '/admin/editTable',
						onSubmit : function() {//------------------------------------------------------------
							//$.parser.parse(paraValueList);
							if(flag1&&flag2&&flag3){
								var row=paraDefLis.datagrid('getSelected');
								deleteAllValues(row.PARA_ID);
							}
							return $(this).form('validate');
						},
					success : function(data) {
						if (data == 'ok') {
							$("#ParaDefLis").datagrid("reload");
							$('#eidtParamDialog').dialog("close");
							messageSuccess(data);
						} else {
							
							messageFail(data);
								}
							}
						});
					}
				}]		
		});
		
		
		
		//触发修改参数弹窗
		var btnEidtParam=$("#btnEidtParam").bind('click',function(){
			var row=paraDefLis.datagrid('getSelected');
			if(row){
				$("#paraId").val(row.PARA_ID);
				$("#paraName").val(row.PARA_NAME);
				$("#paraDesc").val(row.PARA_DESC);
				$("#singleParaValue").val(row.SINGLE_PARA_VALUE);
				$("#dataType").combobox({
					url:'/admin/dataType',    
				    valueField:'DATA_TYPE',    
				    textField:'TYPE_NAME' , 
					width:50,
					panelHeight:'auto',
					onLoadSuccess : function(obj) {
						$.each(obj, function(index, oneObj) {//obj是一个下拉列表值得集合
							if (oneObj.DATA_TYPE == row.DATA_TYPE) {		//默认选中行
								$("#dataType").combobox("setValue", oneObj.DATA_TYPE);
							}
						});
						}
				
				});
							
				$("#systemAdmin").combobox({
					url:'/admin/yesOrNo',    
				    valueField:'YES_NO',    
				    textField:'YES_NO' , 
					width:50,
					panelHeight:'auto',
					onLoadSuccess : function(obj) {
						$.each(obj, function(index, oneObj) {//obj是一个下拉列表值得集合
							if (oneObj.YES_NO == row.SYSTEM_ADMIN) {		//默认选中行
								$("#systemAdmin").combobox("setValue", oneObj.YES_NO);
							}
						});
						},
					
				});
				var organRela;
				organRela=$("#organRela").combobox({//-----------------------------------------------------------
					url:'/admin/yesOrNo',    
				    valueField:'YES_NO',    
				    textField:'YES_NO' , 
					width:50,
					panelHeight:'auto',
					onLoadSuccess : function(obj) {
						$.each(obj, function(index, oneObj) {//obj是一个下拉列表值得集合
							if (oneObj.YES_NO == row.ORGAN_RELA) {		//默认选中行
								$("#organRela").combobox("setValue", oneObj.YES_NO);
							}
						});
						if(row.ORGAN_RELA=="Y"){
							$("#singleParaValue").attr("disabled",true);
							if(row.DEPT_RELA=="Y"&&row.TIME_RELA=="Y"){
								$("#btnAddValue").linkbutton('disable');
							}
							flag1=false;
						}else{
							flag1=true;
							}
						
						
						
						
						},
					onSelect:function(obj){
							$('#organRela').val(obj.YES_NO);
								if ($('#organRela').val() == "N") {		
									flag1=true;
									$(".organIds").attr("disabled",true);
								}else{
									flag1=false;
									$(".organIds").attr("disabled",false);
								}
							if(flag1 && flag2 && flag3){
								$("#singleParaValue").attr("disabled",false);
								$("#btnAddValue").linkbutton('disable');
								//deleteAllValues(row.PARA_ID);
								$('#paraValueList').datagrid('loadData', { total: 0, rows: [] });//清空DateGrid 
								
							}else{
								$("#singleParaValue").attr("disabled",true);
								$("#btnAddValue").linkbutton('enable');
							}
						}
				}); 
				$("#deptRela").combobox({
					url:'/admin/yesOrNo',    
				    valueField:'YES_NO',    
				    textField:'YES_NO' , 
					width:50,
					panelHeight:'auto',
					onLoadSuccess : function(obj) {
						$.each(obj, function(index, oneObj) {//obj是一个下拉列表值得集合
							if (oneObj.YES_NO == row.DEPT_RELA) {		//默认选中行
								$("#deptRela").combobox("setValue", oneObj.YES_NO);
							}
						});
						if(row.DEPT_RELA=="Y"){
							$("#singleParaValue").attr("disabled",true);
							if(row.ORGAN_RELA=="Y"&&row.TIME_RELA=="Y"){
								$("#btnAddValue").linkbutton('disable');
							}
							 flag2=false;
							
					}else{
						flag2=true;
					}
						},
					onSelect: function(obj){ 
						$('#deptRela').val(obj.YES_NO);
						if ($('#deptRela').val() == "N") {		
							flag2=true;
							$(".deptIds").attr("disabled",true);
						}else{
							flag2=false;
							$(".deptIds").attr("disabled",false);
						}
						if(flag1 && flag2 && flag3){
							$("#singleParaValue").attr("disabled",false);
							$("#btnAddValue").linkbutton('disable');
							//deleteAllValues(row.PARA_ID);
							$('#paraValueList').datagrid('loadData', { total: 0, rows: [] });//清空DateGrid 
							
						}else{
							$("#singleParaValue").attr("disabled",true);
						    $("#btnAddValue").linkbutton('enable');
						}
					
					}
				}); 
				$("#timeRela").combobox({
					url:'/admin/yesOrNo',    
				    valueField:'YES_NO',    
				    textField:'YES_NO' , 
					width:50,
					panelHeight:'auto',
					onLoadSuccess : function(obj) {
						$.each(obj, function(index, oneObj) {//obj是一个下拉列表值得集合
							if (oneObj.YES_NO == row.TIME_RELA) {		//默认选中行
								$("#timeRela").combobox("setValue", oneObj.YES_NO);
							}
						});
						if(row.TIME_RELA=="Y"){
							$("#singleParaValue").attr("disabled",true);
							if(row.ORGAN_RELA=="Y"&&row.DEPT_RELA=="Y"){
								$("#btnAddValue").linkbutton('disable');
							}
							flag3=false;
							
						}else{
							flag3=true;
						}
						},
					onSelect: function(obj){ 
						$('#timeRela').val(obj.YES_NO);
						if ($('#timeRela').val() == "N") {		
							flag3=true;
							$(".timeIds").attr("disabled",true);
						}else{
							flag3=false;
							$(".timeIds").attr("disabled",false);
						}
						if(flag1 && flag2 && flag3){
							$("#singleParaValue").attr("disabled",false);
							$("#btnAddValue").linkbutton('disable');
							//deleteAllValues(row.PARA_ID);
							$('#paraValueList').datagrid('loadData', { total: 0, rows: [] });//清空DateGrid 
							
						}else{
							$("#singleParaValue").attr("disabled",true);
							$("#btnAddValue").linkbutton('enable');
						}
					
					}
				}); 
				
				if(row.ORGAN_RELA=="N"&&row.DEPT_RELA=="N"&&row.TIME_RELA=="N"){
					$("#btnAddValue").linkbutton('disable');
				}
				//获取子表信息
				paraValueList.datagrid({
					url:'/admin/allValue',
						queryParams: {
							'paraId':row.PARA_ID
							}
				});
				
				eidtParamDialog.dialog('open');
			}
		});
		
		//$('#startDate,#endDate').datebox();
		
		
		//value字表信息
		var paraValueList;
		paraValueList=$("#paraValueList").datagrid({
	  		fit : true,
			fitColumns : true,
	  		striped:true,
	  		singleSelect:'true',
			idField : 'valueId',
			rownumbers : true,
			onLoadSuccess:function(obj){
				if(flag1)
					$(".organIds").attr("disabled",true);
				if(flag2)
					$(".deptIds").attr("disabled",true);
				if(flag3)
					$(".timeIds").attr("disabled",true);
			},
			columns : [ [
			{field:'checkbox',title:'check',width:10, checkbox:true },
			
			{field:'VALUE_ID', title:'valueId',
				formatter:function(value,row,index){
					return row.VALUE_ID+"<input type='hidden' name='valueId' value='"+row.VALUE_ID+"'/>";
						} 		
			},
			{field:'PARA_VALUE', title:'paraValue',width:40,
				formatter:function(value,row,index){
					if(row.PARA_VALUE!=null)
					return "<input type='text' name='paraValue' value='"+row.PARA_VALUE+"'/>";
					else
					return "<input type='text' name='paraValue'/>";
						} 	
			},
			{field:'ORGAN_ID', title:'organId',width:40,
				formatter:function(value,row,index){
					if(row.ORGAN_ID!=null)
					return "<input type='text' name='organId' class='organIds' value='"+row.ORGAN_ID+"'/>";
					else
					return "<input type='text' class='organIds' name='organId'/>";
						} 	
			},
			{field:'DEPT_ID', title:'deptId',width:40,
				formatter:function(value,row,index){
					if(row.DEPT_ID!=null)
					return "<input type='text' name='deptId' class='deptIds'  value='"+row.DEPT_ID+"'/>";
					else
					return "<input type='text' name='deptId' class='deptIds' />";
						} 		
			},
			
			{field:'START_DATE', title:'startDate',width:40,
				formatter:function(value,row,index){
					if(row.START_DATE!=null)
					return "<input type='text' name='startDate' class='timeIds'  value='"+row.START_DATE+"'/>";
					else
					return "<input type='text' name='startDate' class='timeIds'/>";


						} 		
			},
			{field:'END_DATE', title:'endDate',width:40,
				formatter:function(value,row,index){
					if(row.END_DATE!=null)
					return "<input type='text' name='endDate' class='timeIds'  value='"+row.END_DATE+"'/>";
					else
					return "<input type='text' name='endDate' class='timeIds' />";
						} 		
			},
				]]
			});			
		
		//增加参数信息弹窗
		var flag4=false;
		var flag5=false;
		var flag6=false;
		var addParamDialog;
		addParamDialog=$("#addParamDialog").dialog({
		title : '添加参数',
		width : fixWidth(0.7),
		height : fixHeight(0.8),
		closed : true,											
		cache : false,
		iconCls : 'icon-add',									
		modal : true,
		resizable:true,
		onClose:function(){						
			$("#addParamForm").form("clear");
			flag4=false;
			flag5=false;
			flag6=false;
			$("#btnAddValue2").linkbutton('enable');
			$("#addValues").children().remove();
		},		
		buttons : [ {							
			iconCls : 'icon-ok',
			text : '提交',
			handler : function() {				
					$("#addParamForm").form('submit', {	
						url : '/admin/addTable',
						onSubmit : function() {
						return $(this).form('validate');
						},
					success : function(data) {
						if (data == 'ok') {
							$("#ParaDefLis").datagrid("reload");
							$('#addParamDialog').dialog("close");
							messageSuccess(data);
						} else {
							
							messageFail(data);
								}
							}
						});
					}
				}]		
		});
		
		//触发增加参数弹窗
		$("#btnAddParam").bind('click',function(){
			$("#dataType2").combobox({
				url:'/admin/dataType',    
			    valueField:'DATA_TYPE',    
			    textField:'TYPE_NAME' , 
				width:50,
				panelHeight:'auto'		
			});
						
			$("#systemAdmin2").combobox({
				url:'/admin/yesOrNo',    
			    valueField:'YES_NO',    
			    textField:'YES_NO' , 
				width:50,
				panelHeight:'auto'
			});
		$("#organRela2").combobox({
				url:'/admin/yesOrNo',    
			    valueField:'YES_NO',    
			    textField:'YES_NO' , 
				width:50,
				panelHeight:'auto',
				onSelect:function(obj){
						$('#organRela2').val(obj.YES_NO);
							if ($('#organRela2').val() == "N") {		
								flag4=true;
							}else{
								flag4=false;
							}
						if(flag4 && flag5 && flag6){
							$("#singleParaValue2").attr("disabled",false);
							$("#btnAddValue2").linkbutton('disable');
							$("#addValues").children().remove();
						}else{
							$("#singleParaValue2").attr("disabled",true);
							$("#btnAddValue2").linkbutton('enable');
						}
					}
			}); 
			$("#deptRela2").combobox({
				url:'/admin/yesOrNo',    
			    valueField:'YES_NO',    
			    textField:'YES_NO' , 
				width:50,
				panelHeight:'auto',
				onSelect: function(obj){ 
					$('#deptRela2').val(obj.YES_NO);
					if ($('#deptRela2').val() == "N") {		
						flag5=true;
					}else{
						flag5=false;
					}
					if(flag4 && flag5 && flag6){
						$("#singleParaValue2").attr("disabled",false);
						$("#btnAddValue2").linkbutton('disable');
						$("#addValues").children().remove();
						
					}else{
						$("#singleParaValue2").attr("disabled",true);
					    $("#btnAddValue2").linkbutton('enable');
					}
				
				}
			}); 
			$("#timeRela2").combobox({
				url:'/admin/yesOrNo',    
			    valueField:'YES_NO',    
			    textField:'YES_NO' , 
				width:50,
				panelHeight:'auto',
				onSelect: function(obj){ 
					$('#timeRela2').val(obj.YES_NO);
					if ($('#timeRela2').val() == "N") {		
						flag6=true;
					}else{
						flag6=false;
					}
					if(flag4 && flag5 && flag6){
						$("#singleParaValue2").attr("disabled",false);
						$("#btnAddValue2").linkbutton('disable');
						$("#addValues").children().remove();
					}else{
						$("#singleParaValue2").attr("disabled",true);
						$("#btnAddValue2").linkbutton('enable');
					}
				
				}
			}); 
			$("#btnAddValue2").linkbutton('enable');
			addParamDialog.dialog('open');
		});
		
		//增加参数弹窗的增加按钮绑定函数
		$("#btnAddValue2").bind('click',function(){
			if(!flag4||!flag5||!flag6){
			var tr = $("<tr><td>paraValue:<input id='paraValue2' type='text' style='width:70px'  name='paraValue2'/></td>" +
			 		"<td>organId:<input id='organId2' type='text' class='organId2' style='width:70px'  name='organId2'/></td>" +
			 		"<td>deptId:<input id='deptId2' type='text' class='deptId2'  style='width:70px'  name='deptId2'/></td>" +
			 		"<td>startDate:<input id='startDate2' class='easyui-datebox' style='width:70px'  name='startDate2'/></td>" +
			 		"<td>endDate:<input id='endDate2' class='easyui-datebox' style='width:70px'  name='endDate2'/></td>" +
			 		"<td><input name='btnRemove2' type='button' value='-'/></td>"+
			 		"</tr>").appendTo("#addValues");
			 
			 if(flag4){
					$(".organId2").attr("disabled",true);
					$(".organId2").val("");
				 }
			 if(flag5){
					$(".deptId2").attr("disabled",true);
					$(".deptId2").val("");
				 }
			 if(flag6){
					$(".easyui-datebox").attr("disabled",true);
					
				 }
			//重新渲染easyUI
			 $.parser.parse(tr);
			
			}
			
		});
	

		 $("#addValues").delegate("input[name='btnRemove2']", "click", function() {
            $(this).parent().parent().remove();
        });
		 
		// 修改参数弹窗中增加单个参数弹窗
		var addSingleValueDialog=$("#addSingleValueDialog").dialog({
			title : '增加value',
			width : fixWidth(0.6),
			height : fixHeight(0.3),
			closed : true,											
			cache : false,
			iconCls : 'icon-add',									
			modal : true,
			resizable:true,
			onClose:function(){						
				$("#addValueForm").form("clear");
			},		
			buttons : [ {							
				iconCls : 'icon-ok',
				text : '提交',
				handler : function() {				
						$("#addValueForm").form('submit', {	
							url : '/admin/addSingleValue',
							onSubmit : function() {
							return $(this).form('validate');
							},
						success : function(data) {
							if (data == 'ok') {
								$("#paraValueList").datagrid("reload");
								$('#addSingleValueDialog').dialog("close");
								messageSuccess(data);
							} else {
								
								messageFail(data);
									}
								}
							});
						}
					}]		
			
		}); 
		
		//触发修改参数弹窗中增加单个参数弹窗的按钮的函数
		$("#btnAddValue").bind('click',function(){
			var row=paraDefLis.datagrid('getSelected');
			$("#paraId3").val(row.PARA_ID);
			if(!flag1||!flag2||!flag3){
			addSingleValueDialog.dialog("open");
			
			if(flag1){
				$("#organId3").attr("disabled",true);
			}
			if(flag2){
				$("#deptId3").attr("disabled",true);
			}
			if(flag2){
				$("#startDate3").attr("disabled",true);
				$("#endDate3").attr("disabled",true);
			}
			
			}
		});
		
		
		
		
		
		
});	
	
//----------------------------------------------------------------	
//删除所有value值得函数
function deleteAllValues(paraId){
	$.ajax({
		url:"/admin/delAllValues",
		type:"post",
		data:{paraId:paraId}, 	
		dataType:"text",
		success:function(data){
			if(data=='ok')
			{
				$.messager.show({
				title:'提示',
				msg:'关联信息已删除',
				timeout:3000,
				showType:'slide'
				});
			}else{
				$.messager.show({
				title:'提示',
				msg:'关联信息删除失败！'+data,
				timeout:3000,
				showType:'slide'
					});
				}
		}
		
		});
}


	


//操作成功滑框
function  messageSuccess(data) {
		$.messager.show( {		
		title : '我的消息',
		msg : '操作成功！',
		timeout : 3000,		
		showType : 'slide'	
		});
	} 
//操作失败滑框
function messageFail(data){
		$.messager.show( {		
		title : '消息提示',
		msg : data,
		timeout : 5000,		
		showType : 'slide'	
		});
	}

//网页可见区域宽百分比转换
function fixWidth(percent)  
{  
    return document.body.clientWidth * percent ; //这里你可以自己做调整  
}  
//网页可见区域高百分比转换
function fixHeight(percent)  
{  
    return document.body.clientHeight * percent ; //这里你可以自己做调整  
}  


